using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Magiq.Support {
  public static class CollectionExtensions {
    public static bool IsNotEmpty<T>(this IEnumerable<T> enumerable) {
      return !enumerable.IsEmpty();
    }

    public static bool IsEmpty<T>(this IEnumerable<T> enumerable) {
      return enumerable.FirstOrDefault() == null;
    }

    public static bool IsEmpty<T>(this ICollection<T> collection) {
      return collection.Count == 0;
    }

    public static bool IsNotEmpty<T>(this ICollection<T> collection) {
      return !collection.IsEmpty();
    }

    public static string Join<T>(this IEnumerable<T> enumerable, string separator, Func<T, string> converter) {
      return string.Join(separator, enumerable.Select(converter).ToArray());
    }

    public static string Join<T>(this IEnumerable<T> enumerable, string separator) {
      return enumerable.Join(separator, x => x.ToString());
    }

    public static void ForEach<T>(this IEnumerable items, Action<T> action) {
      foreach (T item in items)
        action(item);
    }

    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> items, Action<T> action) {
      foreach (var item in items)
        action(item);

      return items;
    }

    public static bool In<T>(this T item, IList<T> items) {
      return items.Contains(item);
    }

    public static bool NotIn<T>(this T item, IList<T> items) {
      return !item.In(items);
    }

    public static IList<T> ReplaceWith<T>(this IList<T> list, IEnumerable<T> other) {
      list.Clear();
      other.ForEach(list.Add);
      return list;
    }

    public static bool Matches<T>(this IEnumerable<T> list1, IEnumerable<T> list2) {
      var list = list2.ToList();
      return list1.Any(s => s.In(list));
    }
  }
}